package leetcodecompetition.D358;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: Siantar
 * @Date: 2023-08-13-11:11
 * @Description: 1.0
 */
public class T7022 {
    public int minAbsoluteDifference(List<Integer> nums, int x) {
        int n = nums.size();
        int res = Integer.MAX_VALUE;
        int[] arr = new int[10_0000_0001];
        Arrays.fill(arr, Integer.MAX_VALUE);
        for (int i = x; i < n; i++) {
            int num = nums.get(i);
            int p = nums.get(i - x);
            add(arr, p);
            res = Math.min(res, arr[num]);
        }
        return res;
    }

    public void add(int[] arr, int a) {
        for (int i = a; i < arr.length; i++) {
            if (arr[i] < i - a) {
                break;
            }
            arr[i] = i - a;
        }
        for (int i = a; i >= 0; i--) {
            if (arr[i] < a - i) {
                break;
            }
            arr[i] = a - i;
        }
    }
}
